//
//  AddViewController.swift
//  app路由
//
//  Created by zhaoguoteng on 2020/4/27.
//  Copyright © 2020 zhaoguoteng. All rights reserved.
//

import UIKit
import FMDB

class AddViewController: UITableViewController {
    
    @IBOutlet var nameField: UITextField!
    @IBOutlet var urlTextView: UITextView!
    @IBOutlet var remarkTextView: UITextView!
    @IBOutlet var imgView: UIImageView!
    
    @IBOutlet var tipsView: UIView!
    @IBOutlet var tipsLabel: UILabel!
    
    var route : Route?
    var imgName: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
        loadDBData()
    }
    
    func setupView() {
        
        if let route = route {
            nameField.text = route.name
            urlTextView.text = route.url
            remarkTextView.text = route.remark
            imgView.image = ImageFileManager.imageWithFileName(route.img)
        }
        
        tableView.tableHeaderView = nil
    }
    
    func loadDBData() {
    }
    
    @IBAction func saveAction() {
        
        guard let db = SqliteManager.shareDB else {
            return
        }
        
        if !db.isOpen {
            db.open()
        }
        
        guard let name = nameField.text, let url = urlTextView.text else {
            tipsLabel.text = "数据不完整"
            tableView.tableHeaderView = tipsView
            return
        }
        
        guard let testUrl = URL(string: url), testUrl.scheme != nil else {
            tipsLabel.text = "链接格式不正确"
            tableView.tableHeaderView = tipsView
            return
        }
        
        tipsLabel.text = nil
        tableView.tableHeaderView = nil
        
        let remark = self.remarkTextView.text ?? ""
        
        do {
            if let route = route, let rid = route.rid {
                try db.executeUpdate("update routes SET name = '\(name)', url = '\(url)', remark = '\(remark)', img = '\(imgName ?? "")' where rid = \(rid)", values: nil)
            } else {
                try db.executeUpdate("insert into routes (name, url, remark, img, date) values ('\(name)', '\(url)', '\(remark)', '\(imgName ?? "")', '\(Date().timeIntervalSince1970)')",
                    values: nil)
            }
            navigationController?.popViewController(animated: true)
        } catch {
            fatalError(error.localizedDescription)
        }
        
        db.close()
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 3 {
     /// 选择图片
            let imagePickerVC = UIImagePickerController()
            imagePickerVC.delegate = self
            present(imagePickerVC, animated: true, completion: nil)
        }
    }
}

extension AddViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        if let img = info[.originalImage] as? UIImage{
            imgView.image = img
            imgName = ImageFileManager.saveImageToLocal(image: img)
        }
        dismiss(animated: true, completion: nil)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
}
